/**
 * 
 */
package com.cqan.util;

import java.awt.Color;
import java.util.Random;

/**
 * @author slaton
 * 
 *         下午01:48:31
 */
public class CodeUtils {
	static String[] code = { "0","1","2","3","4","5","6","7","8","9",
							 "A","B","C","D","E","F","G","H","I","J","K","L","M",
							 "N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
							 "a","b","c","d","e","f","g","h","i","j","k","l","m",
							 "n","o","p","q","r","s","t","u","v","w","x","y","z"};

	/**
	 * 获取随机字段串
	 * @param num 位数
	 * @param digital 是否包含数字
	 * @param letter 是否包含字母
	 * @return
	 */
	@SuppressWarnings("unused")
	public static String[] createCode(Integer num,Boolean digital,Boolean letter) {
		String[] sb = new String[num];
		int start = 0;
		int end = 62;
		if (num==null&&num<=0) {
			throw new RuntimeException("num is illegal");
		}
		if (!digital) {
			start = 10;
		}
		if (!letter) {
			end = 9;
		}
		Random rand = new Random();
		for (int i = 0; i < num; i++) {
			int n = (int) rand.nextInt(end-start)+start;
			sb[i]=code[n];
		}
		return sb;
	}
	
	/**
	 * 获取随机颜色
	 * @return
	 */
	public static Color getRandomColor(){
		
		Color[] colors = {Color.BLUE,Color.BLACK,Color.CYAN,Color.GREEN,Color.DARK_GRAY,new Color(164,31,107),new Color(37,31,164),Color.MAGENTA};
		Random rand = new Random();
		return colors[rand.nextInt(colors.length)];
	}
}
